//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "YoYInflationCollar.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/CashFlow.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/YoYInflationTermStructure.h>
#include <gen/QL/Cashflows/YoYInflationCoupon.h>
#include <gen/QL/Instruments/YoYInflationCapFloor.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Cashflows;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CYoYInflationCollar::CYoYInflationCollar (Cephei::IVector<Cephei::QL::ICashFlow^>^ yoyLeg, Cephei::IVector<Double>^ capRates, Cephei::IVector<Double>^ floorRates, Cephei::QL::IPricingEngine^ QL_Pricer) : CYoYInflationCapFloor(CYoYInflationCollar::typeid)
{
    CoVector<Cephei::QL::ICashFlow^>^ _CyoyLeg;
    CoVector<Double>^ _CcapRates;
    CoVector<Double>^ _CfloorRates;
    try
    {
#ifdef HANDLE
        _phYoYInflationCollar = NULL;
#endif
        _CyoyLeg = safe_cast<CoVector<Cephei::QL::ICashFlow^>^> (yoyLeg);
        _CyoyLeg ->Lock ();
        INativeVector<Cephei::QL::ICashFlow^>^ _NCIyoyLeg = _CyoyLeg->getFeature (NativeFeature::shared_ptr);
        CCashFlowVector^ _NCyoyLeg = safe_cast<CCashFlowVector^>(_NCIyoyLeg);
        std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _yoyLeg = static_cast<std::vector<boost::shared_ptr<QuantLib::CashFlow> >&> (_NCyoyLeg->GetShared ());
        CoVector<Double>^ _CcapRates = safe_cast<CoVector<Double>^> (capRates);
        _CcapRates->Lock();
        INativeVector<Double>^ _NCIcapRates = _CcapRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcapRates = safe_cast<CDoubleVector^>(_NCIcapRates);
        std::vector<QuantLib::Rate>& _capRates = static_cast<std::vector<QuantLib::Rate>&> (_NCcapRates->GetReference ());
        CoVector<Double>^ _CfloorRates = safe_cast<CoVector<Double>^> (floorRates);
        _CfloorRates->Lock();
        INativeVector<Double>^ _NCIfloorRates = _CfloorRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfloorRates = safe_cast<CDoubleVector^>(_NCIfloorRates);
        std::vector<QuantLib::Rate>& _floorRates = static_cast<std::vector<QuantLib::Rate>&> (_NCfloorRates->GetReference ());
        _ppYoYInflationCollar = new boost::shared_ptr<QuantLib::YoYInflationCollar> (new QuantLib::YoYInflationCollar ( _yoyLeg,  _capRates,  _floorRates ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppYoYInflationCollar)->setPricingEngine (_QL_Pricer);
        SetYoYInflationCapFloor (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationCollar));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CyoyLeg != nullptr) _CyoyLeg->Unlock();
        if (_CcapRates != nullptr) _CcapRates->Unlock();
        if (_CfloorRates != nullptr) _CfloorRates->Unlock();
    }
}
Cephei::QL::Instruments::CYoYInflationCollar::CYoYInflationCollar (boost::shared_ptr<QuantLib::YoYInflationCollar>& childNative, Object^ owner) : CYoYInflationCapFloor(CYoYInflationCollar::typeid)
{
#ifdef HANDLE
	_phYoYInflationCollar = NULL;
#endif
	_ppYoYInflationCollar = &childNative;
    _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationCollar));
}
Cephei::QL::Instruments::CYoYInflationCollar::CYoYInflationCollar (QuantLib::YoYInflationCollar& childNative, Object^ owner) : CYoYInflationCapFloor(CYoYInflationCollar::typeid)
{
#ifdef HANDLE
	_phYoYInflationCollar = NULL;
#endif
	_ppYoYInflationCollar = new boost::shared_ptr<QuantLib::YoYInflationCollar> (&childNative);
    _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationCollar));
    _YoYInflationCollarOwner = owner;
    _YoYInflationCapFloorOwner = owner;
}

Cephei::QL::Instruments::CYoYInflationCollar::CYoYInflationCollar (CYoYInflationCollar^ copy) : CYoYInflationCapFloor(CYoYInflationCollar::typeid)
{
#ifdef HANDLE
	_phYoYInflationCollar = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYInflationCollar = new boost::shared_ptr<QuantLib::YoYInflationCollar> (copy->GetShared());
        _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationCollar));
    }
}
Cephei::QL::Instruments::CYoYInflationCollar::CYoYInflationCollar (System::Type^ t) : CYoYInflationCapFloor(CYoYInflationCollar::typeid)
{
#ifdef HANDLE
	_phYoYInflationCollar = NULL;
#endif
	if (!t->IsSubclassOf(CYoYInflationCollar::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CYoYInflationCollar::CYoYInflationCollar (QuantLib::Handle<QuantLib::YoYInflationCollar>& childNative, Object^ owner)  : CYoYInflationCapFloor(CYoYInflationCollar::typeid)
{
	_phYoYInflationCollar = &childNative;
	_ppYoYInflationCollar = &static_cast<boost::shared_ptr<QuantLib::YoYInflationCollar>>(childNative.currentLink());
    _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationCollar));
    _YoYInflationCollarOwner = owner;
}
Cephei::QL::Instruments::CYoYInflationCollar::CYoYInflationCollar (QuantLib::Handle<QuantLib::YoYInflationCollar> childNative)  : CYoYInflationCapFloor(CYoYInflationCollar::typeid)
{
	_phYoYInflationCollar = &childNative;
	_ppYoYInflationCollar = &static_cast<boost::shared_ptr<QuantLib::YoYInflationCollar>>(childNative.currentLink());
    _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationCollar));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CYoYInflationCollar::CYoYInflationCollar (QuantLib::YoYInflationCollar childNative)  : CYoYInflationCapFloor(CYoYInflationCollar::typeid)
{
#ifdef HANDLE
	_phYoYInflationCollar = NULL;
#endif
	_ppYoYInflationCollar = new boost::shared_ptr<QuantLib::YoYInflationCollar> (new QuantLib::YoYInflationCollar (childNative));
    _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationCollar));
}
#endif

Cephei::QL::Instruments::CYoYInflationCollar::~CYoYInflationCollar ()
{
    if (_ppYoYInflationCollar != NULL)
    {
	    delete _ppYoYInflationCollar;
        _ppYoYInflationCollar = NULL;
    }
}
Cephei::QL::Instruments::CYoYInflationCollar::!CYoYInflationCollar ()
{
    if (_ppYoYInflationCollar != NULL)
    {
	    delete _ppYoYInflationCollar;
    }
}
QuantLib::YoYInflationCollar& Cephei::QL::Instruments::CYoYInflationCollar::GetReference ()
{
    if (_ppYoYInflationCollar == NULL) throw gcnew NativeNullException ();
	return **_ppYoYInflationCollar;
}
boost::shared_ptr<QuantLib::YoYInflationCollar>& Cephei::QL::Instruments::CYoYInflationCollar::GetShared ()
{
    if (_ppYoYInflationCollar == NULL) throw gcnew NativeNullException ();
	return *_ppYoYInflationCollar;
}
QuantLib::YoYInflationCollar* Cephei::QL::Instruments::CYoYInflationCollar::GetPointer ()
{
    if (_ppYoYInflationCollar == NULL) throw gcnew NativeNullException ();
	return &**_ppYoYInflationCollar;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYInflationCollar>& Cephei::QL::Instruments::CYoYInflationCollar::GetHandle ()
{
	if (_phYoYInflationCollar == NULL)
	{
		_phYoYInflationCollar = new Handle<QuantLib::YoYInflationCollar> (*_ppYoYInflationCollar);
	}
	return *_phYoYInflationCollar;
}
#endif
bool Cephei::QL::Instruments::CYoYInflationCollar::HasNative () 
{
	return (_ppYoYInflationCollar != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IYoYInflationCollar^ Cephei::QL::Instruments::CYoYInflationCollar_Factory::Create (Cephei::IVector<Cephei::QL::ICashFlow^>^ yoyLeg, Cephei::IVector<Double>^ capRates, Cephei::IVector<Double>^ floorRates, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CYoYInflationCollar ( yoyLeg,  capRates,  floorRates,  QL_Pricer);
}
